package com.shadow.demo.info.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.shadow.demo.data.info.entity.SiteInfoEntity;
import com.shadow.demo.excel.demo.v35.Base35Listener;
import com.shadow.demo.info.service.SiteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class Site35Listener extends Base35Listener<SiteService, SiteInfoEntity, SiteVO> {

    private String userId;

    public Site35Listener(final SiteService service) {
        super(service);
    }

    public Site35Listener(
            final SiteService service,
            final int batchCount,
            final String userId
    ) {
        super(service, batchCount);
        if (StringUtils.isBlank(userId)) {
            throw new IllegalArgumentException("用户id不能为空");
        }
        this.userId = userId;
    }

    /**
     * 处理一整行数据
     * @param data 一行数据
     *             等同于 {@link AnalysisContext#readRowHolder()}
     * @param context excel内容
     */
    @Override
    public void invoke(final SiteVO data, final AnalysisContext context) {
        Boolean checked = check(context.readRowHolder().getRowIndex(), data);
        if (Boolean.TRUE.equals(checked)) {
            SiteInfoEntity convert = data.convert();
            convert.setPart(userId);
            cacheList.add(convert);
        }
        index++;
        if (index >= batchCount) {
            submit();
        }
    }
}
